package com.renjun.smweb.controller.EnrollmentController;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.annotation.AuthorityDeclaration;
import com.renjun.smweb.commons.componet.FileIOComponent;
import com.renjun.smweb.commons.global.DataMap;
import com.renjun.smweb.commons.global.FileInfoPo;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.service.EnrollmentService.EnrollmentService;
import com.renjun.smweb.annotation.AuthorityDeclaration;
import com.renjun.smweb.commons.componet.FileIOComponent;
import com.renjun.smweb.commons.global.DataMap;
import com.renjun.smweb.commons.global.FileInfoPo;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.exceptions.*;
import com.renjun.smweb.service.EnrollmentService.EnrollmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Map;

/**
 * Created by RenJun on 2017/2/21.
 * 招生设置M5
 */
@Controller
@RequestMapping("/enrollment")
public class EnrollmentController {
    @Autowired
    private EnrollmentService enrollmentService;

    /**
     * 招生计划列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/enrollPlanList", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M5-1", desc = "招生计划列表",errMsgs = "找不到相应的数据",reqParam = "")
    public JSONObject enrollPlanList(HttpServletRequest request, HttpServletResponse response){
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        try {
            rs = enrollmentService.enrollPlanList(params);
        } catch (SQLException | DataNotFoundException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 招生计划列表统计
     * @param request
     * @return
     */
    @RequestMapping(value = "/enrollPlanListStat", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M5-7", desc = "招生计划列表统计",errMsgs = "找不到相应的数据",reqParam = "")
    public JSONObject enrollPlanListStat(HttpServletRequest request, HttpServletResponse response){
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        try {
            rs = enrollmentService.enrollPlanListStat(params);
        } catch (SQLException | DataNotFoundException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 招生计划列表导出
     * @param request
     * @return
     */
    @RequestMapping(value = "/expEnrollPlanList", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M5-6", desc = "招生计划列表导出",errMsgs = "找不到相应的数据",reqParam = "")
    public JSONObject expEnrollPlanList(HttpServletRequest request, HttpServletResponse response){
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        try {
            rs = enrollmentService.expEnrollPlanList(params);
        } catch (SQLException | DataNotFoundException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        } catch (FileTypeNotFoundException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 批量导入招生计划
     * @param request
     * @return
     */
    @RequestMapping(value = "/batchUpdateEnrollPlan", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M5-4", desc = "批量导入招生计划",errMsgs = "找不到相应的数据",reqParam = "")
    public JSONObject batchUpdateEnrollPlan(@RequestParam(value = "upfile_attachment", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> params = new DataMap<>();
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        HttpSession session = request.getSession();
        LoginInfo loginInfo = (LoginInfo) session.getAttribute("userinfo");
        try {
            FileInfoPo fileInfoPo = FileIOComponent.createTempFilePathByType(FileIOComponent.IMPORT_TYPE,"xlsx");
            if(loginInfo==null)throw new SystemTimeOutException();
            file.transferTo(fileInfoPo.getFileObject());
            params.put("url", fileInfoPo.getFileAbsPath());//地址
            params.put("schoolcode",loginInfo.getSchoolcode());
            params.put("adminid",loginInfo.getId());
            rs = enrollmentService.batchUpdateEnrollPlan(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 单个修改招生计划
     * @param request
     * @return
     */
    @RequestMapping(value = "/modifyEnrollPlan", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M5-5", desc = "单个修改招生计划",errMsgs = "找不到相应的数据",reqParam = "")
    public JSONObject modifyEnrollPlan(HttpServletRequest request, HttpServletResponse response){
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        try {
            rs = enrollmentService.modifyEnrollPlan(params);
        } catch (SQLException | DataNotFoundException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        } catch (BaseException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 录取信息下载设置
     * @param request {endtime:"2016-07-31"starttime:"2016-07-26"type:"signtime"}
     * @return []
     */
    @RequestMapping(value = "/updateEnrollOpenTime", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M5-2", desc = "录取信息下载设置",reqParam = "starttime,endtime",resBody = "设置成功",errMsgs = "sql异常,没有找到对应数据")
    public JSONObject updateEnrollOpenTime(HttpServletRequest request, HttpServletResponse response)  {
        Map<String, Object> map = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        LoginInfo loginInfo = (LoginInfo) request.getSession().getAttribute("userinfo");
        try {
            if(loginInfo==null)throw new SystemTimeOutException();
            map.put("adminid",loginInfo.getId());
            rs = enrollmentService.updateEnrollOpenTime(map);
        } catch (SQLException | DataNotFoundException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        } catch (ParseException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        } catch (EnrollOpenTimeException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        } catch (SystemTimeOutException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    @RequestMapping(value = "/queryEnrollOpenTime", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M5-3", desc = "录取设置预加载",reqParam = "districtcode",resBody = "startime,endtime",errMsgs = "sql异常,没有找到对应数据")
    public JSONObject queryEnrollOpenTime(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        try {
            rs = enrollmentService.queryEnrollOpenTime(map);
        } catch (SQLException | DataNotFoundException e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }
}
